//
// Created by Administrator on 2021/11/23.
// May Saint Diana bless you!
//
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <climits>

using namespace std;

class Solution {
public:
    bool buddyStrings(string s, string goal) {
        int m = s.size(), n = goal.size();
        if (m != n)
            return false;
        if (s != goal) {
            // 两个字符串不一样，那么就要找到不一样的2个char
            int different = 0;
            vector<char> vs;
            vector<char> vg;
            for (int i = 0; i < m; ++i) {
                if (s[i] != goal[i]) {
                    ++different;
                    vs.push_back(s[i]);
                    vg.push_back(goal[i]);
                }
            }
            if (different != 2 || vs.size() != 2 || vg.size() != 2) {
                return false;
            }
            return vs[0] == vg[1] && vs[1] == vg[0];
        } else {
            // 两个字符串相同，那么找到相同的char就行
            if (m > 26)
                return true;
            vector<int> counter(26, 0);
            for (int i = 0; i < m; ++i) {
                int pos = s[i] - 'a';
                ++counter[pos];
                if (counter[pos] >= 2)
                    return true;
            }
            return false;
        }
    }
};

int main() {
    Solution solution;
    cout << solution.buddyStrings("ab", "ba") << endl;
    cout << solution.buddyStrings("ab", "ab") << endl;
    cout << solution.buddyStrings("aa", "aa") << endl;
    return 0;
}
